package com.hmall.cart.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hmall.cart.domain.po.Cart;
import com.hmall.cart.service.ICartService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
@RequiredArgsConstructor
public class CartStateListener {
    private final ICartService cartService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "cart.clear.queue", durable = "true"),
            exchange = @Exchange(name = "trade.topic"),
            key = "order.create"
    ))
    public void listenPaySuccess(Set< Long> orderId, Message message){
        Object userid = message.getMessageProperties().getHeader("user-info");
        LambdaQueryWrapper<Cart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserId, userid);
        queryWrapper.in(Cart::getItemId, orderId);
        cartService.remove(queryWrapper);
    }
}
